/**
 * Created by huachuan on 2016/1/7.
 */
angular.module('myactivitycontrollers',[])
  //当天活动
  .controller('MyActiviityCtrl',function($scope,$ionicActionSheet,$ionicSlideBoxDelegate,$ionicScrollDelegate,Meeting,Storage,Loading,History,ENV,Toast,ShareServiceOther,ShareService){
      var memberId = '';
      $scope.picPath = ENV.picPath;//图片路径
      $scope.iReleaseList = [];
      $scope.invitedMeList = [];
      $scope.iApplyList = [];
    $scope.isShareShow = ShareService.getIsShowShareWindow();//判断是否显示分享框
    $scope.slideChanged = function(index){
      if(index==0){
        showMeReleaseFun();
        Meeting.getIReleaseList(memberId);
      }else if(index==1){
        showInvitedMeFun();
        Meeting.getInvitedMeList(memberId);
      }if(index==2){
        showSignInFun();
        Meeting.getIApplyList(memberId);
      }
    }

    $scope.doRefreshIRelease = function(){Meeting.getIReleaseList(memberId);}
    $scope.doRefreshInvitedMe = function(){Meeting.getInvitedMeList(memberId);}
    $scope.doRefreshIApply = function(){Meeting.getIApplyList(memberId);}

    //进入页面加载
    var firstIn = true;
    $scope.$on('$ionicView.beforeEnter',function(){
      memberId = Storage.get('member').member_id;
      if($scope.showWhich[0]){
        if(firstIn){
          Loading.showLoading();
          firstIn = false;
        }
        Meeting.getIReleaseList(memberId);
      }else if($scope.showWhich[1]){
        Meeting.getInvitedMeList(memberId);
      }else if($scope.showWhich[2]){
        Meeting.getIApplyList(memberId);
      }
      searchList = [];
      //我发起
      if($scope.showWhich[0] && $scope.iReleaseList!=undefined && $scope.iReleaseList.length>0){
        for(i=0;i<$scope.iReleaseList.length;i++){
          search = {
            mm_id:$scope.iReleaseList[i].mm_id,
            member_id:memberId
          }
          searchList.push(search);
        }
        //邀请我
      }else if($scope.showWhich[1]&&$scope.invitedMeList!=undefined&&$scope.invitedMeList.length>0){
        for(i=0;i<$scope.invitedMeList.length;i++){
          search = {
            mm_id:$scope.invitedMeList[i].mm_id,
            member_id:memberId
          }
          searchList.push(search);
        }
        //已报名
      }else if($scope.showWhich[2]&&$scope.iApplyList!=undefined&&$scope.iApplyList.length>0){
        for(i=0;i<$scope.iApplyList.length;i++){
          search = {
            mm_id:$scope.iApplyList[i].mm_id,
            member_id:memberId
          }
          searchList.push(search);
        }
      }
      if(searchList.length>0){
        Meeting.getCurrentMeetingInfo(searchList);
      }
    });

    $scope.$on('Meeting.getCurrentMeetingInfoUpdated',function(){
      var result = Meeting.getCurrentInfo();
      if($scope.showWhich[0]){
        for(i=0;i<$scope.iReleaseList.length;i++){
          for(j=0;j<result.length;j++){
            if($scope.iReleaseList[i].mm_id==result[j].mm_id){
              $scope.iReleaseList[i].mm_comment_num = result[j].mm_comment_num;
              $scope.iReleaseList[i].lmeetStatus = result[j].lmeetStatus;
              $scope.iReleaseList[i].mcmeetStatus = result[j].mcmeetStatus;
            }
          }
        }
      }else if($scope.showWhich[1]){
        for(i=0;i<$scope.invitedMeList.length;i++){
          for(j=0;j<result.length;j++){
            if($scope.invitedMeList[i].mm_id==result[j].mm_id){
              $scope.invitedMeList[i].mm_comment_num = result[j].mm_comment_num;
              $scope.invitedMeList[i].lmeetStatus = result[j].lmeetStatus;
              $scope.invitedMeList[i].mcmeetStatus = result[j].mcmeetStatus;
            }
          }
        }
      }else if($scope.showWhich[2]){
        for(i=0;i<$scope.iApplyList.length;i++){
          for(j=0;j<result.length;j++){
            if($scope.iApplyList[i].mm_id==result[j].mm_id){
              $scope.iApplyList[i].mm_comment_num = result[j].mm_comment_num;
              $scope.iApplyList[i].lmeetStatus = result[j].lmeetStatus;
              $scope.iApplyList[i].mcmeetStatus = result[j].mcmeetStatus;
            }
          }
        }
      }
    });
    //显示那一个（我发起，邀请我，已报名）
    $scope.showWhich = [true,false,false];
    //加载更多
    $scope.loadMoreIRelease = function(){Meeting.getMoreIReleaseList(memberId);}
    $scope.loadMoreInvitedMe = function(){Meeting.getMoreInvitedMeList(memberId);}
    $scope.loadMoreIApply = function(){Meeting.getMoreIApplyList(memberId);}


    /*$scope.loadMore = function(){
      if($scope.showWhich[0]){
          Meeting.getMoreIReleaseList(memberId);
      }else if($scope.showWhich[1]){
          Meeting.getMoreInvitedMeList(memberId);
      }else if($scope.showWhich[2]){
          Meeting.getMoreIApplyList(memberId);
      }
    }*/
    $scope.iReleaseHasNextPage = function(){
      return Meeting.iReleaseHasNextPage();
    }
    $scope.invitedMeHasNextPage = function(){
      return Meeting.invitedMeHasNextPage();
    }
    $scope.iApplyHasNextPage = function(){
      return Meeting.iApplyHasNextPage();
    }

    /****************我发起*******************/
    $scope.showMeRelease = function(){
      $ionicSlideBoxDelegate.slide(0,300);
      showMeReleaseFun();
    }
    showMeReleaseFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [true,false,false];
    }
    $scope.$on('Meeting.iReleaseActivityUpdated',function(){
      Loading.hideLoading();
      $scope.$broadcast('scroll.refreshComplete');
      $scope.$broadcast('scroll.infiniteScrollComplete');
      $scope.iReleaseList = Meeting.getIReleaseListJson();
      $scope.member = Meeting.getMemberInfo();
    });
    /*****************邀请我****************/
    var firstInInvitedMe = true;
    $scope.showInvitedMe = function(){
      $ionicSlideBoxDelegate.slide(1,300);
      showInvitedMeFun();
    }
    showInvitedMeFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [false,true,false];
      if(firstInInvitedMe){
        Loading.showLoading();
        firstInInvitedMe = false;
      }
    }
    $scope.$on('Meeting.invitedMeActivityUpdated',function(){
      Loading.hideLoading();
      $scope.$broadcast('scroll.refreshComplete');
      $scope.$broadcast('scroll.infiniteScrollComplete');
      $scope.invitedMeList = Meeting.getInvitedMeListJson();
    });
    /*****************已报名****************/
   var firstInSignIn = true;
    $scope.showSignIn = function(){
      $ionicSlideBoxDelegate.slide(2,300);
      showSignInFun();
    }
    showSignInFun = function(){
      $ionicScrollDelegate.scrollTop();
      $scope.showWhich = [false,false,true];
      if(firstInSignIn){
        firstInSignIn = false;
        Loading.showLoading();
      }
    }
    $scope.$on('Meeting.iApplyActivityUpdated',function(){
      Loading.hideLoading();
      $scope.$broadcast('scroll.refreshComplete');
      $scope.$broadcast('scroll.infiniteScrollComplete');
      $scope.iApplyList = Meeting.getIApplyListJson();
    });
    //返回
    $scope.goback = function(){
      History.goback();
    }
    /******************点赞，或者收藏******************/
    var collectIndex = '';//收藏的索引
    var likeIndex = '';//点赞的索引
    $scope.collectMeeting = function(index,mmId){
        Meeting.collectMeeting(memberId,mmId);
        collectIndex = index;
        Loading.showLoading();
    }
    //网络访问成功后
    $scope.$on('Meeting.collectMeetingUpdated', function(){
      Loading.hideLoading();
      var result = Meeting.getCollectJson();
      if($scope.showWhich[0]){
        $scope.iReleaseList[collectIndex].mcmeetStatus = result.mcmeetStatus;
      }else if($scope.showWhich[1]){
        $scope.invitedMeList[collectIndex].mcmeetStatus = result.mcmeetStatus;
      }else if($scope.showWhich[2]){
        $scope.iApplyList[collectIndex].mcmeetStatus = result.mcmeetStatus;
      }
      Toast.showMessage(result.mcmeetMsg);
      collectIndex='';
    })
    $scope.likeMeeting = function(index,mmId){
      Meeting.likeMeeting(memberId,mmId);
      likeIndex = index;
      Loading.showLoading();
    }
    //网络访问成功后
    $scope.$on('Meeting.likeMeetingUpdated', function(){
      Loading.hideLoading();
      var result = Meeting.getLikeJson();
      if($scope.showWhich[0]){
        $scope.iReleaseList[likeIndex].lmeetStatus = result.lmeetStatus;
      }else if($scope.showWhich[1]){
        $scope.invitedMeList[likeIndex].lmeetStatus = result.lmeetStatus;
      }else if($scope.showWhich[2]){
        $scope.iApplyList[likeIndex].lmeetStatus = result.lmeetStatus;
      }
      Toast.showMessage(result.lmeetMsg);
      likeIndex='';
    });


    /* ========================分享约局按钮  begin======================= */
    //显示约局分享框
    $scope.showMeetingShare = function(mm_id){
      ShareServiceOther.showShareWindow(mm_id,"meeting");
    };
    /* =========================分享约局按钮  end =============================== */
  });

